package com.csdn.leon.adapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

import com.csdn.leon.activity.BlogHostActivity;
import com.csdn.leon.model.CloumnModel;
import com.csdn.leon.util.Constants;
import com.csdn.leon.util.SPUtils;
import com.csdn.leon.util.StringUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.csdn.leon.base.R;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CloumnAdapter extends BaseAdapter{


	private final String TAG = BlogAdapter.class.getSimpleName().toString();

	public static final int STATE_EMPTY_ITEM = 0;
	public static final int STATE_LOAD_MORE = 1;
	public static final int STATE_NO_MORE = 2;
	public static final int STATE_NO_DATA = 3;
	public static final int STATE_LESS_ONE_PAGE = 4;
	public static final int STATE_NETWORK_ERROR = 5;
	public static final int STATE_OTHER = 6;

	protected int state = STATE_LESS_ONE_PAGE;

	protected int _loadmoreText;
	protected int _loadFinishText;
	protected int _noDateText;
	protected int mScreenWidth;

	private Context context;
	private LayoutInflater mInflater;
	private List<CloumnModel> list;

	private LinearLayout mFooterView;
	private DisplayImageOptions options;

	public CloumnAdapter(Context context) {
		this.context = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (list != null) {
			setList(list);
		} else {
			list = new ArrayList<CloumnModel>();
		}
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.csdn)
		.showImageForEmptyUri(R.drawable.csdn)
		.showImageOnFail(R.drawable.csdn)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(20)).build();
		_loadmoreText = R.string.loading;
		_loadFinishText = R.string.loading_no_more;
		_noDateText = R.string.error_view_no_data;
	}

	protected LayoutInflater getLayoutInflater(Context context) {
		if (mInflater == null) {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		return mInflater;
	}

	public void setScreenWidth(int width) {
		mScreenWidth = width;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getState() {
		return this.state;
	}

	static class ViewHolder {
		TextView title;
		ImageView img;
		TextView description;
		TextView source;
		TextView time;
		TextView comment_count;
		TextView cloumn_name;
		ImageView tip;

		public ViewHolder(View view) {
			ButterKnife.inject(this, view);
		}
	}

	protected View getRealView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null || convertView.getTag() == null) {
			convertView = getLayoutInflater(parent.getContext()).inflate(
					R.layout.list_cell_firstblog, null);
			vh = new ViewHolder(convertView);
			vh.title = (TextView) convertView.findViewById(R.id.tv_title);
			vh.img = (ImageView) convertView.findViewById(R.id.iv_description);
			vh.description = (TextView) convertView
					.findViewById(R.id.tv_description);
			vh.time = (TextView) convertView.findViewById(R.id.tv_time);
			vh.source = (TextView) convertView.findViewById(R.id.tv_source);
			vh.comment_count = (TextView) convertView
					.findViewById(R.id.tv_comment_count);
			vh.cloumn_name= (TextView)convertView.findViewById(R.id.tv_cloumn_name);
			vh.tip = (ImageView) convertView.findViewById(R.id.iv_tip);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		final CloumnModel blog = list.get(position);

		vh.tip.setVisibility(View.VISIBLE);
		// if (blog.getDocumenttype() == Blog.DOC_TYPE_ORIGINAL) {
		// vh.tip.setImageResource(R.drawable.widget_original_icon);
		// } else {
		// vh.tip.setImageResource(R.drawable.widget_repaste_icon);
		// }

		vh.title.setText(blog.getTitle());

		// if (AppContext.isOnReadedPostList(BlogList.PREF_READED_BLOG_LIST,
		// blog.getId() + "")) {
		// vh.title.setTextColor(parent.getContext().getResources()
		// .getColor(R.color.main_gray));
		// } else {
		// vh.title.setTextColor(parent.getContext().getResources()
		// .getColor(R.color.main_black));
		// }

		vh.description.setVisibility(View.GONE);
		String description = blog.getContent();
		// if (null != description && !StringUtils.isEmpty(description)) {
		vh.description.setVisibility(View.VISIBLE);
		vh.description.setText(description.trim());
		// }
		//
		vh.source.setText(blog.getUsername());
		vh.time.setText(blog.getDate().toString());
		vh.cloumn_name.setText(blog.getClumnName());
		vh.cloumn_name.setVisibility(View.VISIBLE);
		// vh.time.setText(StringUtils.friendly_time(blog.getPubDate()));
		//vh.comment_count.setText(blog.get() + "");
		if ((boolean) SPUtils.get(context, Constants.KEY_LOAD_IMAGE,
				true)) {
			 ImageLoader.getInstance().displayImage(blog.getImageLink(), vh.img, options); 
		} 
		 ImageLoader.getInstance().displayImage(blog.getImageLink(), vh.img, options);  
		 vh.img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent =new Intent(context,BlogHostActivity.class);
				intent.putExtra("username", blog.getUsername());
				context.startActivity(intent);				
			}
		});
		return convertView;
	}

	public List<CloumnModel> getList() {
		return list;
	}

	public void setList(List<CloumnModel> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void addList(List<CloumnModel> list) {
		this.list.addAll(list);
		notifyDataSetChanged();
	}
    
	public void clear(){
		this.list.clear();
	}
	
	@Override
	public int getCount() {
		switch (getState()) {
		case STATE_EMPTY_ITEM:
			return getDataSize() + 1;
		case STATE_NETWORK_ERROR:
		case STATE_LOAD_MORE:
			return getDataSize() + 1;
		case STATE_NO_DATA:
			return 1;
		case STATE_NO_MORE:
			return getDataSize() + 1;
		case STATE_LESS_ONE_PAGE:
			return getDataSize();
		default:
			break;
		}
		return getDataSize();
	}

	public int getDataSize() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == getCount() - 1) {
			Log.w(TAG, "position:" + position);
			// 最后一条
			// if (position < _data.size()) {
			// position = getCount() - 2; // footview
			// }
			if (getState() == STATE_LOAD_MORE || getState() == STATE_NO_MORE
					|| state == STATE_EMPTY_ITEM
					|| getState() == STATE_NETWORK_ERROR) {
				this.mFooterView = (LinearLayout) LayoutInflater.from(
						parent.getContext()).inflate(R.layout.list_cell_footer,
						null);
				// if (!loadMoreHasBg()) {
				mFooterView.setBackgroundDrawable(null);
				// }
				ProgressBar progress = (ProgressBar) mFooterView
						.findViewById(R.id.progressbar);
				TextView text = (TextView) mFooterView.findViewById(R.id.text);
				switch (getState()) {
				case STATE_LOAD_MORE:
					setFooterViewLoading();
					break;
				case STATE_NO_MORE:
					mFooterView.setVisibility(View.VISIBLE);
					progress.setVisibility(View.GONE);
					text.setVisibility(View.VISIBLE);
					text.setText(_loadFinishText);
					break;
				case STATE_EMPTY_ITEM:
					progress.setVisibility(View.GONE);
					mFooterView.setVisibility(View.VISIBLE);
					text.setText(_noDateText);
					break;
				case STATE_NETWORK_ERROR:
					mFooterView.setVisibility(View.VISIBLE);
					progress.setVisibility(View.GONE);
					text.setVisibility(View.VISIBLE);
					// if (TDevice.hasInternet()) {
					// text.setText("加载出错了");
					// } else {
					text.setText("没有可用的网络");
					// }
					break;
				default:
					progress.setVisibility(View.GONE);
					mFooterView.setVisibility(View.GONE);
					text.setVisibility(View.GONE);
					break;
				}
				return mFooterView;
			}
		}
		if (position < 0) {
			position = 0; // 若列表没有数据，是没有footview/headview的
		}
		return getRealView(position, convertView, parent);
	}

	public void setFooterViewLoading(String loadMsg) {
		ProgressBar progress = (ProgressBar) mFooterView
				.findViewById(R.id.progressbar);
		TextView text = (TextView) mFooterView.findViewById(R.id.text);
		mFooterView.setVisibility(View.VISIBLE);
		progress.setVisibility(View.VISIBLE);
		text.setVisibility(View.VISIBLE);
		if (StringUtils.isEmpty(loadMsg)) {
			text.setText(_loadmoreText);
		} else {
			text.setText(loadMsg);
		}
	}

	public void setFooterViewLoading() {
		setFooterViewLoading("");
	}

	public View getFooterView() {
		return this.mFooterView;
	}

	public void setFooterViewText(String msg) {
		ProgressBar progress = (ProgressBar) mFooterView
				.findViewById(R.id.progressbar);
		TextView text = (TextView) mFooterView.findViewById(R.id.text);
		mFooterView.setVisibility(View.VISIBLE);
		progress.setVisibility(View.GONE);
		text.setVisibility(View.VISIBLE);
		text.setText(msg);
	}

}
